package pageObject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import utils.ReportWriter;

import java.util.Random;

public class RegistrationPage extends BasePage {

    protected static final By registrationLoginInputName = By.name("title");
    protected static final By registrationEmailInputName = By.name("email");
    protected static final By registrationPasswordInputName = By.name("password");
    protected static final By registrationButtonClassName = By.className("button-css-green");

    protected RegistrationPage(WebDriver driver) {
        super(driver);
    }

    public UserPage registerUser(String name, String mail, String password, boolean positiveness) {
        driver.findElement(registrationLoginInputName).clear();
        driver.findElement(registrationLoginInputName).sendKeys(name);

        String email;
        if (positiveness) {
            Random generator = new Random();
            int i = generator.nextInt(100000);
            email = i + mail;
        }
        else {
            email = mail;
        }
        ReportWriter.info("Current email is " + email);


        driver.findElement(registrationEmailInputName).clear();
        driver.findElement(registrationEmailInputName).sendKeys(email);
        driver.findElement(registrationPasswordInputName).clear();
        driver.findElement(registrationPasswordInputName).sendKeys(password);
        driver.findElement(registrationButtonClassName).click();

        if(positiveness) {
            return new UserPage(driver);
        }
        else {
            return null;
        }
    }
}
